package acs;

import io.netty.util.CharsetUtil;

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.*;

public class NioMain {

    static Map<String, SocketChannel> channelMap = new HashMap<>();

    public static void main(String[] args) throws Exception {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        ServerSocket socket = serverSocketChannel.socket();
        socket.bind(new InetSocketAddress(5000));
        Selector selector = Selector.open();

        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        Iterator<SelectionKey> iterator = null;

        System.out.println("服务端启动成功");
        while (true) {
            int select = selector.select();
            Set<SelectionKey> selectedKeys = selector.selectedKeys();
            iterator = selectedKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();
                SocketChannel client = null;
                if (selectionKey.isAcceptable()) {
                	System.out.println("Acceptablethread:"+Thread.currentThread().getName());
                    ServerSocketChannel channel = (ServerSocketChannel) selectionKey.channel();
                    client = channel.accept();
                    client.configureBlocking(false);
                    client.register(selector, SelectionKey.OP_READ);
                    channelMap.put(UUID.randomUUID().toString(), client);
                } else if (selectionKey.isReadable()) {
                	System.out.println("isReadablethread:"+Thread.currentThread().getName());
                    client = (SocketChannel) selectionKey.channel();
                    ByteBuffer byteBuffer = ByteBuffer.allocate(512);
                    int read = client.read(byteBuffer);
                    if (read > 0) {
                        byteBuffer.flip();
                        String message = new String(byteBuffer.array(), CharsetUtil.UTF_8);
                        System.out.println(client + "=" + message);
                        String senderKey = null;
                        for (Map.Entry<String, SocketChannel> entry : channelMap.entrySet()) {
                            if (client == entry.getValue()) {
                                senderKey = entry.getKey();
                                break;
                            }
                        }
                        ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
                        for (Map.Entry<String, SocketChannel> entry : channelMap.entrySet()) {
                            writeBuffer.clear();
                            SocketChannel value = entry.getValue();
                            writeBuffer.put((senderKey + ":" + message).getBytes());
                            writeBuffer.flip();
                            value.write(writeBuffer);
                        }

                    }

                }
                iterator.remove();
            }
        }


    }


}
